package com.ruoyi.common.utils.lpr;


import com.ruoyi.common.result.ResultInfo;
import com.ruoyi.common.utils.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
 * 文件读取工具类
 */
@Slf4j
public class FileUtil {

    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    public static File uploadFile(MultipartFile file) throws IOException {
        String originalName = file.getOriginalFilename();
        log.debug("文件为：{}",originalName);
        String suffix = originalName.substring(originalName.lastIndexOf("."));
        if (originalName.endsWith(".jpg") || originalName.endsWith(".png")) {
            String fileSavePath = System.getProperty("user.dir") + "\\images\\uploadFile";
            File files = new File(fileSavePath);
            if (!files.exists()) {
                files.mkdirs();
            }
            String targetFile = files.getAbsolutePath();
            String newFileName = UUID.randomUUID().toString().replace("-", "") + suffix;
            File newFile = new File(targetFile, newFileName);
            file.transferTo(newFile);
            return newFile;
        }
        throw new CustomException(20001, ResultInfo.UPLOAD_FILE_JPG_OR_PNG);
    }

    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static boolean deleteFile(String filePath) {
        if (filePath == null) {
            return false;
        }
        File file = new File(filePath);
        File[] files = file.listFiles();
        for (File ff : files) {
            if (ff.exists() && ff.isFile()) {
                ff.delete();
            } else {
                deleteFile(ff.getAbsolutePath());
            }
        }
        return true;
    }

    /**
     * 读取文件内容，作为字符串返回
     */
    public static String readFileAsString(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        }

        if (file.length() > 1024 * 1024 * 1024) {
            throw new IOException(ResultInfo.UOLOAD_FILE_ISBIG);
        }

        StringBuilder sb = new StringBuilder((int) (file.length()));
        // 创建字节输入流  
        FileInputStream fis = new FileInputStream(filePath);
        // 创建一个长度为10240的Buffer
        byte[] bbuf = new byte[10240];
        // 用于保存实际读取的字节数  
        int hasRead = 0;
        while ((hasRead = fis.read(bbuf)) > 0) {
            sb.append(new String(bbuf, 0, hasRead));
        }
        fis.close();
        return sb.toString();
    }

    /**
     * 根据文件路径读取byte[] 数组
     */
    public static byte[] readFileByBytes(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(new FileInputStream(file));
                short bufSize = 1024;
                byte[] buffer = new byte[bufSize];
                int len1;
                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
                    bos.write(buffer, 0, len1);
                }

                byte[] var7 = bos.toByteArray();
                return var7;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException var14) {
                    var14.printStackTrace();
                }

                bos.close();
            }
        }
    }
}
